<?php

/**
 * Description of G_Memcached
 *
 * @author Monk <eoe2005@gmail.com>
 */
if (class_exists('Memcached') === false)
{
    trigger_error('系统不支持Memcached模块');
    die();
}

class G_Memcached extends GC_Cache
{

    protected $_memcached;

    public function __construct($servers)
    {
        $this->_memcached = new Memcached;
        $this->_memcached->addServers($servers);
    }

    public function dec($key, $step = 1)
    {
        return $this->_memcached->decrement($key, $step);
    }

    public function delete($keys)
    {
        if (is_array($keys))
        {
            return $this->_memcached->deleteMulti($keys);
        }
        return $this->_memcached->delete($keys);
    }

    public function exists($keys)
    {
        if (is_array($keys))
        {
            $rs = array();
            foreach ($keys AS $k)
            {
                if ($this->_memcached->get($k))
                {
                    $rs[] = $k;
                }
            }
            return $rs;
        } else
        {
            return $this->_memcached->get($key) ? true : false;
        }
    }

    public function flushAll()
    {
        return $this->_memcached->flush();
    }

    public function get($key)
    {
        $r = $this->_memcached->get($key);
        if ($r)
        {
            $r = unserialize($r);
        }
        return $r;
    }

    public function inc($key, $step = 1)
    {
        return $this->_memcached->increment($key, $step);
    }

    public function set($key, $val, $timeout = 0)
    {
        return $this->_memcached->set($key, $val, $timeout);
    }

}
